home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / actlib10.zip / TRACE.H < prev    next >
C/C++ Source or Header  |  1993-02-04  |  3KB  |  122 lines

  1. /***
  2.  *  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)
  3.  *
  4.  *  File    : trace.h
  5.  *
  6.  *  Description    : General macros for tracing
  7.  *                Print all the begining and ending of functions
  8.  *                (indented) with return values.
  9.  *
  10.  *  OS/Compiler : All
  11.  *
  12.  *  Usage       : - Add the line BEGIN( function_name, return_format );
  13.  *                  just after all the declaration of the function.
  14.  *                  return_format is one of the C standard format
  15.  *                  ( "%d", "%s", "%f", ... )
  16.  *
  17.  *                - To trace a value call the function 'trace( (...) );'
  18.  *                  where (...) will be given as is to printf.
  19.  *
  20.  *                - Define 'TRACE' to actually perform tracing,
  21.  *                  do not define it to suppress tracing.
  22.  *
  23.  ***/
  24.  
  25.  
  26. #ifndef _TRACE_H
  27. #define _TRACE_H
  28.  
  29.  
  30.  
  31.         /*  TYPES DEFINITIONS  */
  32.  
  33.  
  34. extern int    errno;
  35. extern char*    strerror( int );
  36. extern void    _exit( int );
  37.  
  38.  
  39.  
  40.         /*  MACROS DEFINITIONS  */
  41.  
  42.  
  43. #ifdef TRACE    /*  For debugging  */
  44.  
  45. extern int    G_nesting;
  46.  
  47. #define main \
  48.     extern int G_nesting = 0; \
  49.     main
  50.  
  51. #define trace( args ) \
  52.     printf( "%*d : ", G_nesting, G_nesting );    \
  53.     printf args
  54.  
  55. #define BEGIN( function, ret_format ) \
  56.     char _FUNCTION_[] = #function;                \
  57. /*    char _RET_FORMAT_[] = #ret_format;*/              \
  58.     char _RET_FORMAT_[] = ret_format;              \
  59.     G_nesting ++;                                            \
  60.     printf( "\n" );                                \
  61.     trace( ("<<< Begin of function " #function "\n" ) );    \
  62.     trace( (" << in file " __FILE__ "\n") );        \
  63.     trace( (" << at line %d\n\n", __LINE__) )
  64.  
  65. #define exit( status ) \
  66.     { printf( "\n" );                        \
  67.       trace( (">>> Exit of function %s\n", _FUNCTION_) );        \
  68.       trace( (" >> in file " __FILE__ "\n") );            \
  69.       trace( (" >> at line %d\n\n", __LINE__) );            \
  70.       trace( (" >> exit code : '" #status "' = %d", status) );    \
  71.       G_nesting --;                                \
  72.       exit( status );                        \
  73.     }
  74.  
  75. #define _exit( status ) \
  76.     { printf( "\n" );                        \
  77.       trace( (">>> Exit of function %s\n", _FUNCTION_) );        \
  78.       trace( (" >> in file " __FILE__ "\n") );            \
  79.       trace( (" >> at line %d\n\n", __LINE__) );            \
  80.       trace( (" >> exit code : '" #status "' = %d", status) );    \
  81.       G_nesting --;                                \
  82.       _exit( status );                        \
  83.     }
  84.  
  85. #define return( value ) \
  86.     { char line[256];                            \
  87.       printf( "\n" );                            \
  88.       trace( (">>> Return from function %s\n", _FUNCTION_) );        \
  89.       trace( (" >> in file " __FILE__ "\n") );                \
  90.       trace( (" >> at line %d\n\n", __LINE__) );                \
  91.       sprintf( line, " >> return code : '" #value "' = %s\n\n", _RET_FORMAT_ ); \
  92.       trace( (line, value) );                        \
  93.       G_nesting --;                                    \
  94.       return( value );                            \
  95.     }
  96.  
  97. #define syserror( error ) \
  98.     { printf( "\n!!! %s\n", strerror(errno) );        \
  99.       printf( " !! in file     : " __FILE__ "\n" );            \
  100.       printf( " !! in function : %s\n", _FUNCTION_ );    \
  101.       printf( " !! at line     : %d\n", __LINE__ );            \
  102.       printf( " !! instruction : " #error "\n\n" );            \
  103.     }
  104.  
  105. #else
  106.  
  107. #define BEGIN( function, ret_format )
  108.  
  109. #define trace( args )
  110.  
  111. #define syserror( error ) \
  112.     { printf( "\n!!! %s\n", strerror(errno) );        \
  113.       printf( " !! in file     : " __FILE__ "\n" );            \
  114.       printf( " !! at line     : %d\n", __LINE__ );            \
  115.       printf( " !! instruction : " #error "\n\n" );            \
  116.     }
  117.  
  118. #endif
  119.  
  120.  
  121. #endif
  122.